Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use per-tx escrow for Wei balance #1314

Merged
merged 2 commits into from
Feb 13, 2024
Merged

Use per-tx escrow for Wei balance #1314

merged 2 commits into from
Feb 13, 2024

Conversation

codchen
Copy link
Collaborator

@codchen codchen commented Feb 1, 2024

Describe your changes and provide context

In order to prevent the Wei escrow from being a bottleneck in OCC, this PR changes the escrow account to one-per-tx, and settle those per-tx escrows to the global escrow in EndBlock. One complication with such setup is that the per-tx escrow may temporarily go negative (because the usei that needs to be redeemed may be deposited in another tx), so a corresponding change in sei-cosmos that skips negative balance check for escrow account has also been made sei-protocol/sei-cosmos#417

Testing performed to validate your change

unit tests with different escrow balances

@codchen codchen requested a review from udpatil February 1, 2024 08:05
@@ -167,6 +167,7 @@
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
evmTxDeferredInfoList := am.keeper.GetEVMTxDeferredInfo(ctx)
am.keeper.SettleWeiEscrowAccounts(ctx, evmTxDeferredInfoList)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
if err := k.BankKeeper().SendCoinsFromAccountToModule(ctx, escrow, banktypes.WeiEscrowName, sdk.NewCoins(seiBalance)); err != nil {
ctx.Logger().Error(fmt.Sprintf("failed to send %s from escrow %d to global escrow", seiBalance.String(), info.TxIndx))
// This should not happen in any case. We want to halt the chain if it does
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
if err := k.BankKeeper().SendCoinsFromModuleToAccount(ctx, banktypes.WeiEscrowName, escrow, sdk.NewCoins(settleAmt)); err != nil {
ctx.Logger().Error(fmt.Sprintf("failed to send %s from global escrow to escrow %d", settleAmt.String(), info.TxIndx))
// This should not happen in any case. We want to halt the chain if it does
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
escrow := state.GetTempWeiEscrowAddress(sdk.Context{}.WithTxIndex(info.TxIndx))
seiBalance := k.BankKeeper().GetBalance(ctx, escrow, denom)
if !seiBalance.Amount.IsZero() {
panic(fmt.Sprintf("failed to settle escrow account %d which still has a balance of %s", info.TxIndx, seiBalance.String()))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@@ -167,6 +167,7 @@ func (am AppModule) BeginBlock(sdk.Context, abci.RequestBeginBlock) {
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
evmTxDeferredInfoList := am.keeper.GetEVMTxDeferredInfo(ctx)
am.keeper.SettleWeiEscrowAccounts(ctx, evmTxDeferredInfoList)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the future, if we allow evm txs into the prioritized execution section, is it necessary to settle the balances after EACH execution group, or is it fine to simply settle in endblock?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine to settle in the endblock. Technically I don't think we ever need to settle, but having negative balances may break some UI (if someone deliberately query the escrow accounts) so we settle it, but as long as end-of-block state is settled we should be ok

func GetTempWeiEscrowAddress(ctx sdk.Context) sdk.AccAddress {
txIndexBz := make([]byte, 8)
binary.BigEndian.PutUint64(txIndexBz, uint64(ctx.TxIndex()))
return sdk.AccAddress(append(WeiTmpEscrowPrefix, txIndexBz...))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a guarantee that this cannot conflict with other user addresses? I'm assuming it wont because there are fewer bytes used in generating the Acc Address?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the bytes are different

@@ -26,6 +27,12 @@ func GetCoinbaseAddress(ctx sdk.Context) sdk.AccAddress {
return sdk.AccAddress(append(CoinbaseAddressPrefix, txIndexBz...))
}

func GetTempWeiEscrowAddress(ctx sdk.Context) sdk.AccAddress {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just use txIndex as an argument instead of Context?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea good point

denom := k.GetBaseDenom(ctx)
// settle surplus escrow first
for _, info := range evmTxDeferredInfoList {
escrow := state.GetTempWeiEscrowAddress(sdk.Context{}.WithTxIndex(info.TxIndx))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment on utils file, might be easier to simply pass txIndex instead of creating an empty context WITH a txindex?

@codchen codchen force-pushed the evm-wei-balance-para branch 2 times, most recently from 8cb3ab4 to 1d9f623 Compare February 13, 2024 03:11
Copy link

codecov bot commented Feb 13, 2024

Codecov Report

Attention: 15 lines in your changes are missing coverage. Please review.

Comparison is base (83e6885) 63.30% compared to head (1953b9f) 63.15%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##              evm    #1314      +/-   ##
==========================================
- Coverage   63.30%   63.15%   -0.16%     
==========================================
  Files         349      350       +1     
  Lines       23522    23560      +38     
==========================================
- Hits        14891    14879      -12     
- Misses       7787     7833      +46     
- Partials      844      848       +4     
Files Coverage Δ
aclmapping/evm/mappings.go 88.98% <100.00%> (ø)
x/evm/module.go 71.25% <100.00%> (+0.36%) ⬆️
x/evm/state/balance.go 75.55% <100.00%> (ø)
x/evm/state/utils.go 100.00% <100.00%> (ø)
x/evm/state/statedb.go 43.42% <75.00%> (+0.17%) ⬆️
x/evm/keeper/keeper.go 83.83% <20.00%> (-2.06%) ⬇️
x/evm/keeper/wei.go 62.96% <62.96%> (ø)

... and 3 files with indirect coverage changes

@codchen codchen force-pushed the evm-wei-balance-para branch from 1d9f623 to 1953b9f Compare February 13, 2024 03:22
@codchen codchen merged commit 2ddfcd9 into evm Feb 13, 2024
39 checks passed
@codchen codchen deleted the evm-wei-balance-para branch February 13, 2024 04:09
udpatil pushed a commit that referenced this pull request Feb 28, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Feb 28, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Feb 28, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Mar 4, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Mar 4, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Mar 11, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Mar 26, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 17, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 17, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 17, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 18, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 19, 2024
* Use per-tx escrow for Wei balance

* rebase
udpatil pushed a commit that referenced this pull request Apr 19, 2024
* Use per-tx escrow for Wei balance

* rebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants